<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.LinkedList"%>
<%@ page import="com.google.appengine.api.users.User"%>
<%@ page import="com.google.appengine.api.users.UserService"%>
<%@ page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreService"%>
<%@ page import="com.google.appengine.api.datastore.Query"%>
<%@ page import="com.google.appengine.api.datastore.Entity"%>
<%@ page import="com.google.appengine.api.datastore.FetchOptions"%>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory"%>
<%@ page import="com.google.appengine.api.datastore.Text"%>
<%@ page import="com.google.appengine.api.oauth.OAuthRequestException"%>
<%@ page import="com.google.appengine.api.oauth.OAuthService"%>
<%@ page import="com.google.appengine.api.oauth.OAuthServiceFactory"%>
<%@ page import="com.google.appengine.api.oauth.OAuthServiceFailureException"%>
<%@ page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@ page import="edu.dhbw.mannheim.Admin"%>
<%@ page import="edu.dhbw.mannheim.Datastore"%>
<%@ page import="java.util.Date"%>

<%
	String adminNav = "";
	String newPostNav = "";
	String login = "";
	String welcomeMessage = "";
	String body = "";

	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser(); 

	if (user != null) {
		login = "<a href="
				+ userService.createLogoutURL(request.getRequestURI())
				+ ">Sign out</a>";
		welcomeMessage = "Hello, " + user.getNickname()
				+ ". Your are signed in.";
		
		// set some content, that not everybody can see
		if (Admin.isAdmin(user.getEmail())) {
			adminNav = "<a href='blog.jsp?type=admin'>Admin</a><br />";
		}
		if (Admin.canPost(user.getEmail())) {
			newPostNav = "<a href='blog.jsp?type=newpost'>New Blog</a><br />";
		}
	} else {
		login = "<a href="
				+ userService.createLoginURL(request.getRequestURI())
				+ ">Sign in</a>";
		welcomeMessage = "Hello guest! Be welcomed on this blog. Feel free to post comments.";
	}
	
%>
<html>
<head>
<title>blogblogblog</title>
<meta itemprop="image" content="http://blog-blog-blog.appspot.com/images/blogblogblogLogo.png">
<html itemscope itemtype="http://schema.org/Blog">
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
<script type="text/javascript" src="/js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="/js/blog.js"></script>
<body onload="setInterval('scroll();', 1000);">
	<table class="all fullwidth">
		<tr>
			<!-- Header-->
			<td class="leftBar">
				<div class="logoDiv">
					<a href="blog.jsp"><img class="logo" src="images/blogblogblogLogo.png"></img></a>
				</div> <!-- Navigation bar on the left -->
				<div>
					<div class="round">
						<a href="blog.jsp">Home</a><br /> 
						<%=newPostNav%> 
						<a href="blog.jsp?type=listposts">List All Posts</a><br /> <a href="blog.jsp?type=aboutus">Impressum</a><br />
						<%=adminNav%>
						<!--<a href="javascript:randomPost()">Random post</a>-->
					</div>
					<div class="round" id="search">
						Search<br />
						<form>
							<input id="searchInput" class="verySmall round fullwidth textarea" type="text" onKeyUp="search(this.value)"></input>
						</form>
					</div>
					<!-- field for displaying search results -->
					<div id="previewField"></div>
				</div>
			</td>

			<!-- Content field -->
			<td class="rightBar">
				<div class="small round">
					<table class="fullwidth">
						<tr>
							<td class="left"><%=welcomeMessage%></td>
							<td class="right"><%=login%></td>
						</tr>
					</table>
				</div>
				<div class="round" id="contentField">
					<%
						String type = "";
						if (request.getParameter("type") != null) {
							type = request.getParameter("type");
						}
						if (type.equals("newpost") && Admin.canPost(user.getEmail())) {
					%><jsp:include page="newPost.jsp" />
					<%
						} else if (type.equals("aboutus")) {
					%><jsp:include page="about.jsp" />
					<%
						} else if (type.equals("listposts")) {
					%><jsp:include page="listposts.jsp" />
					<%
						} else if (type.equals("admin") && Admin.isAdmin(user.getEmail())) {
					%><jsp:include page="admin.jsp" />
					<%
						} else {
							if (request.getParameter("id") == null) {
								Entity post = Datastore.getLatestPost();
								if(post != null) {
									int newestPost = Integer.parseInt(post.getProperty(
											"internalID").toString());
									String redirectURL = "blog.jsp?id=" + newestPost;
									response.sendRedirect(redirectURL);
								}
							} else {
					%>
					<jsp:include page="showPost.jsp">
						<jsp:param name="id" value="<%=request.getParameter(\"id\")%>" />
					</jsp:include>
					<%
						}
					}
					%>
				</div>
			</td>
		</tr>
	</table>
</body>
</html>

